<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
    String date = input.getDesc("date", "");
    Integer serviceId = input.getInt("server_id", -1);
    List<Map> services = QBuilder.db("basedb").select("new map(id as server_id, name as name, parentId as parent_id, price  as price)").from("Service").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", serviceId)).query().getResultObj();
    if ("".equals(date)) {
        msg = new Message(30002, "请选择预约时间");
        Tools.encryptPrint(out, msg);
        return;
    }
    if (isEmpty(services)) {
        msg = new Message(30041, "服务异常");
        Tools.encryptPrint(out, msg);
        return;
    }
    Map map = services.get(0);
	Date d = DateUtil.parseDate(date);
	SimpleDateFormat sdf = new SimpleDateFormat("E");
	String week = sdf.format(d);
    List<ServiceSchedule> schedules = QBuilder.db("basedb").select().from("ServiceSchedule").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("date=", week).and("serviceId=", serviceId)).query().getResultObj();
    List<User> users = QBuilder.db("basedb").select().from("User").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("type=", 200)).query().getResultObj();
    List<Map> experts = new ArrayList<Map>();
    List<String> expertIds = new ArrayList<String>();
    for (int i = 0; i < schedules.size(); i++) {
        ServiceSchedule ss = schedules.get(i);
        Map expert = new HashMap();
        String doctorUid = Tools.getDesc(ss.getDoctorUid()+"", "-1");
        String doctorName = Tools.getDesc(ss.getDoctorName(), "");
        String price = Tools.getDesc(ss.getPrice() + "", "");
        String half = Tools.getDesc(ss.getHalf() + "", "");
        String id = Tools.getDesc(ss.getId() + "", "");
        expert.put("doctor_uid", doctorUid);
        expert.put("doctor_name", doctorName);
        expert.put("price", price);
        expert.put("half", half);
        expert.put("schedule_id", id);
        for (int j = 0; j < users.size(); j++) {
            User user = users.get(j);
            String _id = Tools.getDesc(user.getId() + "", "");
            if (_id.equals(doctorUid)) {
                expert.put("avatar", Tools.getDesc(user.getAvatar(), ""));
            }
        }
        experts.add(expert);
        if (!expertIds.contains(doctorUid)) {
            expertIds.add(doctorUid);
        }
    }
    map.put("schedules", experts);
    List<Map> normals = new ArrayList<Map>();
    for (int i = 0; i < users.size(); i++) {
        User user = users.get(i);
        String id = Tools.getDesc(user.getId() + "", "");
        if (expertIds.contains(id)) {
            continue;
        }
        String doctorName = Tools.getDesc(user.getName(), "");
        Map expert = new HashMap();
        expert.put("doctor_uid", id);
        expert.put("doctor_name", doctorName);
        expert.put("avatar", Tools.getDesc(user.getAvatar(), ""));
        normals.add(expert);
    }
    map.put("normal", normals);
    msg.put("data", map);
    Tools.encryptPrint(out, msg);
%>
